// -----------------------------------------------------------------------
//  <copyright file="AutoMapperMapper.cs" company="LiuliuSoft">
//      Copyright (c) 2022-2024 DaprPlus. All rights reserved.
//  </copyright>
//  <site>https://dapr.plus</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2024/12/5 14:29:19</last-date>
// -----------------------------------------------------------------------
using IMapper = DaprPlus.Mapping.IMapper;

namespace DaprPlus.AutoMapper;

/// <summary>
/// AutoMapper映射执行类
/// </summary>
public class AutoMapperMapper(MapperConfiguration configuration) : IMapper
{
    private readonly global::AutoMapper.IMapper _mapper = configuration.CreateMapper();

    /// <summary>
    /// 将对象映射为指定类型
    /// </summary>
    /// <typeparam name="TTarget">要映射的目标类型</typeparam>
    /// <param name="source">源对象</param>
    /// <returns>目标类型的对象</returns>
    public TTarget MapTo<TTarget>(object source)
    {
        return _mapper.Map<TTarget>(source);
    }

    /// <summary>
    /// 使用源类型的对象更新目标类型的对象
    /// </summary>
    /// <typeparam name="TSource">源类型</typeparam>
    /// <typeparam name="TTarget">目标类型</typeparam>
    /// <param name="source">源对象</param>
    /// <param name="target">待更新的目标对象</param>
    /// <returns>更新后的目标类型对象</returns>
    public TTarget MapTo<TSource, TTarget>(TSource source, TTarget target)
    {
        return _mapper.Map(source, target);
    }

    /// <summary>
    /// 将数据源映射为指定输出DTO的集合
    /// </summary>
    /// <typeparam name="TOutputDto">输出DTO类型</typeparam>
    /// <param name="source">数据源</param>
    /// <param name="membersToExpand">成员展开</param>
    /// <returns>输出DTO的结果集</returns>
    public IQueryable<TOutputDto> ToOutput<TOutputDto>(IQueryable source,
        params Expression<Func<TOutputDto, object>>[] membersToExpand)
    {
        return source.ProjectTo(configuration, membersToExpand);
    }
}
